<?php
class Cart extends CI_Controller{
	public function index(){
		
		$this->template->write_view('center','front_end/cart/index',null,TRUE);
		$this->template->write_view('menu','front_end/layout/menu',null,TRUE);
		$this->template->write_view('menu_footer','front_end/layout/menu_footer',null,TRUE);
		$this->template->load();
	}
	
	//Ajax
	public function add_cart_item($id){
		if($this->Cart_model->validate_add_cart_item($id)){
			redirect('cart');
		} else {
			echo 'false';
		}
	} 
	
	
	public function update_cart_item(){
		
		$curl = $this->lang->lang() == 'vi' ? 1 : $this->utility->currency('USD','VND','1');
		
		$message = '';
		$total_cost = '';
		$total_sub = '';
		if($this->Cart_model->validate_update_cart_item()){
			$message = 'true';
			$total_cost =  $this->cart->format_number( round($this->cart->total() / $curl, 4) );
			$total_sub = $this->cart->format_number( round($_GET['price'] * $_GET['qty'] / $curl, 4) ) ;
		} 
		else{
			$message = 'false';
		
		}
		$output = '{"message":"'.$message.'","cost":"'.$total_cost.'","sub":"'.$total_sub.'"}';
		echo $output;
	}
	
	public function remove_cart_item(){
	if($this->Cart_model->validate_remove_cart_item()){
			echo $this->cart->format_number($this->cart->total());
	/*	echo '1';*/
		} 
		else{
			echo 'false';
		}
	}
	
	public function empty_cart(){
		try {
			$this->cart->destroy();
			echo 'TRUE';	
		} catch (Exception $e) {
			echo 'FALSE';
		}
		
	}
	public function insertOrderIsStatusPending()
	{
		
		$u = $this->session->userdata('username');
		$temp = $this->Cart_model->validate_checkout($u);
		if($temp)
		{
			echo 'true';
		}
		else {
			echo 'false';
		}
			
	}
		
		
	
}